package com.ysd.lis.controller.lab;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.ysd.common.Result;
import com.ysd.lis.annotation.MainInfoLog;
import com.ysd.lis.aspect.OperationType;
import com.ysd.lis.controller.pub.BaseController;
import com.ysd.lis.dto.lab.MaininfoDto;
import com.ysd.lis.entity.lab.LabCriticalReportDto;
import com.ysd.lis.entity.lab.LabMaininfo;
import com.ysd.lis.entity.lab.LabRptresult;
import com.ysd.lis.mapper.lab.LabRptresultMapper;
import com.ysd.lis.request.AddLabMaininfo;
import com.ysd.lis.request.DeleteResultParam;
import com.ysd.lis.request.Params;
import com.ysd.lis.request.RptResultDto;
import com.ysd.lis.service.bak.LabRptresultBakService;
import com.ysd.lis.service.lab.LabRptresultService;
import com.ysd.lis.service.lab.impl.LabRptresultCalcServiceImpl;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 检验业务表 前端控制器
 * </p>
 *
 * @author Bai
 * @since 2023-12-26
 */
@RestController
@RequestMapping("/lab/rptResult")
public class LabRptresultController extends BaseController {

    @Autowired
    LabRptresultService labRptresultService;

    @Autowired
    LabRptresultCalcServiceImpl labRptresultCalcService;

    @Autowired
    LabRptresultMapper labRptresultMapper;
    @Autowired
    LabRptresultBakService LabRptresultBakService;

    @ApiOperation("新增检查项目")
    @PostMapping("/insertRptResult")
    @MainInfoLog(operationType = OperationType.INSERT, module = "检验报告", content = "新增检验项目", businessName = "rptResult")
    public Result insertRptResult(@RequestBody LabRptresult labRptresult) throws JsonProcessingException {
        return labRptresultService.insertRptResult(labRptresult);
    }

    @ApiOperation("保存图文报告项目编辑值")
    @PostMapping("/saveImgTxtRptValue")
    @MainInfoLog(operationType = OperationType.INSERT, module = "检验报告", content = "保存图文报告项目编辑值", businessName = "rptResult")
    public Result saveImgTxtRptValue(@RequestBody LabRptresult labRptresult) throws JsonProcessingException {
        return labRptresultService.saveImgTxtRptValue(labRptresult);
    }

    @ApiOperation("批量保存图文报告项目编辑值")
    @PostMapping("/saveImgTxtRptValueBatch")
    @MainInfoLog(operationType = OperationType.INSERT, module = "检验报告", content = "批量保存图文报告项目编辑值", businessName = "rptResult")
    public Result saveImgTxtRptValueBatch(@RequestBody List<LabRptresult> labRptresults) throws JsonProcessingException {
        return labRptresultService.saveImgTxtRptValueBatch(labRptresults);
    }


    @ApiOperation("新增一条mianinfo主表记录")
    @GetMapping("/addMaininfoByReqNo")
    @MainInfoLog(operationType = OperationType.INSERT, module = "检验报告", content = "新增一条mianinfo主表记录", businessName = "labMaininfo")
    public Result addMaininfoByReqNo(AddLabMaininfo labMaininfo) throws JsonProcessingException {
        return labRptresultService.addMaininfoByReqNo(labMaininfo);
    }

    // @ApiOperation("删除检查项目")
    // @PostMapping("/deleteRptResult")
    // public Result deleteRptResult(@RequestBody LabRptresult labRptresult) {
    //     return labRptresultService.deleteRptResult(labRptresult);
    // }

    @ApiOperation("删除检查项目")
    @PostMapping("/deleteRptResults")
    @MainInfoLog(operationType = OperationType.DELETE, module = "检验报告", content = "删除检验项目", businessName = "rptResult")
    public Result deleteRptResults(@RequestBody DeleteResultParam deleteResultParam) {
        return labRptresultService.deleteRptResults(deleteResultParam);
    }

    @ApiOperation("修改结果")
    @PostMapping("/updateRptResult")
    @MainInfoLog(operationType = OperationType.UPDATE, module = "检验报告", content = "修改检验结果", businessName = "rptResult")
    public Result updateRptResult(@RequestBody LabRptresult labRptresult) throws JsonProcessingException {
        return labRptresultService.updateRptResult(labRptresult);
    }


    @ApiOperation("批量修改结果")
    @PostMapping("/updateResult")
    @MainInfoLog(operationType = OperationType.UPDATE, module = "检验报告", content = "修改检验结果", businessName = "rptResult")
    public Result updateResult(@RequestBody LabMaininfo labMaininfo)  {
        return labRptresultService.updateResult(labMaininfo);
    }



    @ApiOperation("批量修改结果")
    @PostMapping("/updateRptResultBatch")
    @MainInfoLog(operationType = OperationType.UPDATE, module = "检验报告", content = "修改检验结果", businessName = "rptResult")
    public Result updateRptResultBatch(@RequestBody List<LabRptresult> list) throws JsonProcessingException {
        LabRptresult labRptresult = new LabRptresult();
        Result result = null;
        String mainInfoId = "";
        if (CollectionUtils.isNotEmpty(list)) {
            result = labRptresultService.updateRptResult(list.get(0));
            labRptresult = (LabRptresult) result.getData();
            mainInfoId = labRptresult.getMaininfoId();
            //查询一下当前的数据
            LambdaQueryWrapper<LabRptresult> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(LabRptresult::getMaininfoId, mainInfoId).eq(LabRptresult::getDelFlag, 0);
            List<LabRptresult> list1 = labRptresultService.list(lambdaQueryWrapper);
            for (int i = 1; i < list.size(); i++) {
                list.get(i).setMaininfoId(mainInfoId);
                int finalI = i;
                List<LabRptresult> collect = list1.stream().filter(a -> a.getItemNo().equals(list.get(finalI).getItemNo())).collect(Collectors.toList());
                if (CollectionUtils.isEmpty(collect)) {
                    labRptresultService.save(list.get(i));
                }
            }
        }
        return Result.succ(labRptresult);
    }

    @ApiOperation("批量修改结果")
    @PostMapping("/updateRptResultBatchNew")
    @MainInfoLog(operationType = OperationType.UPDATE, module = "检验报告", content = "修改检验结果", businessName = "rptResult")
    public Result updateRptResultBatchNew(@RequestBody List<LabRptresult> list) throws JsonProcessingException {
        return LabRptresultBakService.updateRptResultBatchNew(list);
    }


    @ApiOperation("修改复查结果")
    @PostMapping("/updateRepetition")
    public Result updateRepetition(@RequestBody LabRptresult labRptresult) {
        return labRptresultService.updateRepetition(labRptresult);
    }

    @ApiOperation("获取检测项目")
    @GetMapping("/findRptResultItem")
    public Result findRptResultItem(Params params) {
        return labRptresultService.findRptResultItem(params);
    }


    @ApiOperation("获取图文报告模板项目")
    @GetMapping("/findRptResultItemByTemp")
    public Result findRptResultItemByTemp(Params params) {
        return labRptresultService.findRptResultItemByTemp(params);
    }


    @ApiOperation("根据模板ID获取图文报告模板项目")
    @GetMapping("/findRptResultItemByTempId")
    public Result findRptResultItemByTempId(Params params) {
        return labRptresultService.findRptResultItemByTempId(params);
    }

    @ApiOperation("根据模板ID获取图文报告模板项目")
    @GetMapping("/upDataRptResultByTempId")
    public Result upDataRptResultByTempId(Params params) {
        return labRptresultService.upDataRptResultByTempId(params);
    }

    @ApiOperation("根据模板ID获取图文报告文字结果")
    @GetMapping("/findLabTemplateDetailByTempId")
    public Result findLabTemplateDetailByTempId(String templateId) {
        return labRptresultService.findLabTemplateDetailByTempId(templateId);
    }

    @ApiOperation("获取所有检测项目")
    @GetMapping("/findRptResultItemAll")
    public Result findRptResultItemAll(Params params) {
        return labRptresultService.findRptResultItemAll(params);
    }

    @ApiOperation("根据标本号查询检验结果")
    @GetMapping("/findReviewedResutByMaininfoId")
    public Result findReviewedResutBySampleNo(@RequestParam String maininfoId) {
        return labRptresultService.findReviewedResutBySampleNo(maininfoId);
    }


    @ApiOperation("批量新增")
    @PostMapping("/batchAddResultItemList")
    public Result batchAddResultItemList(@RequestBody RptResultDto dto) {
        return labRptresultService.batchAddResultItemList(dto);
    }

    @ApiOperation("获取是危急值的检验结果")
    @GetMapping("/getAlarmResult")
    public Result getAlarmResult(LabCriticalReportDto criticalReportDto) {
        return labRptresultService.getAlarmResultByMainId(criticalReportDto);
    }

    @ApiOperation("查询是否有未处理的危急值")
    @PostMapping("/getAlarmExistsByMainInfoIds")
    public Result getAlarmExistsByMainInfoIds(@RequestBody List<String> ids) {
        return labRptresultService.getAlarmExistsByMainInfoIds(ids);
    }

    @ApiOperation("获取条码跟踪检验项目数据信息")
    @GetMapping("/getSpecResult")
    public Result getSpecResult(@RequestParam(value = "reqNo") String reqNo,@RequestParam(value = "sampleNo",required = false) String sampleNo) {
        return labRptresultService.getSpecResult(reqNo,sampleNo);
    }

    @ApiOperation("重新计算参考范围")
    @PostMapping("/reCalcRptRssults")
    public Result reCalcRptRssults(@RequestBody LabMaininfo mainInfo) {
//        LambdaQueryWrapper<LabRptresult> queryWrapper = new LambdaQueryWrapper<>();
//        queryWrapper.eq(LabRptresult::getDelFlag, 0);
//        Timestamp timestamp = new Timestamp(mainInfo.getSampleda().getTime());
//        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
//        String formattedDate = sdf.format(timestamp);
//        Timestamp startTime = Timestamp.valueOf(formattedDate + " 00:00:00");
//        Timestamp timestamp1 = new Timestamp(mainInfo.getSampleda().getTime());
//        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
//        String formattedDate1 = sdf1.format(timestamp1);
//        Timestamp endTime = Timestamp.valueOf(formattedDate1 + " 23:59:59");
//        queryWrapper.ge(LabRptresult::getSampleda, startTime);
//        queryWrapper.le(LabRptresult::getSampleda, endTime);
//        queryWrapper.eq(LabRptresult::getRptGroup, mainInfo.getRptGroup());
//        queryWrapper.eq(LabRptresult::getSampleNo, mainInfo.getSampleNo());
//
//        List<LabRptresult> labRptresults = labRptresultMapper.selectList(queryWrapper);
//        return labRptresultCalcService.reCalcReferRange(mainInfo, labRptresults);
        return labRptresultService.reCalcRptRssults(mainInfo);
    }


    @ApiOperation("批次删除")
    @PostMapping("/removeLabRptResultList")
    public Result removeLabRptResultList(@RequestBody RptResultDto resultDto) {
        return labRptresultService.removeLabRptResultList(resultDto);
    }

}
